package com.bilu.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bilu.common.utility.ResponseUtility;
import com.bilu.common.utility.TimeUtility;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.NumberingFormat;
import com.deepoove.poi.data.Numberings;
import com.deepoove.poi.data.ParagraphRenderData;
import com.deepoove.poi.data.Numberings.NumberingBuilder;
import com.deepoove.poi.policy.ParagraphRenderPolicy;

public class BiLuService {
    public static void bilu(JSONObject json, HttpServletResponse response) {
    	System.out.println(json);
    	String beginTime = json.getString("beginTime");
    	String endTime = json.getString("endTime");
    	String address = json.getString("address");
    	JSONArray senderArray = json.getJSONArray("senderList");
    	String inspectObject = json.getString("inspectObject");
    	JSONArray tools = json.getJSONArray("tools");
    	String inspectObjectDesc = json.getString("inspectObjectDesc");
    	JSONArray inspectMethods = json.getJSONArray("inspectMethods");	
    	String inspectResult = json.getString("inspectResult");	
    	String commander = json.getString("commander");
    	String scouter = json.getString("scouter");
    	String recorder = json.getString("recorder");
    	
    	Map<String, Object> data = new HashMap<String, Object>();
    	if(beginTime != null && !beginTime.trim().isEmpty()) {
    		Date beginDate = TimeUtility.getDate(beginTime, "yyyy-MM-dd HH:mm");
    		data.put("beginYear", "   " + TimeUtility.getFormatTime(beginDate, "yyyy") + "   ");
    		data.put("beginMonth", "   " + TimeUtility.getFormatTime(beginDate, "MM") + "   ");
    		data.put("beginDay", "   " + TimeUtility.getFormatTime(beginDate, "dd") + "   ");
    		data.put("beginHour", "   " + TimeUtility.getFormatTime(beginDate, "HH") + "   ");
    		data.put("beginMin", "   " + TimeUtility.getFormatTime(beginDate, "mm") + "   ");
    	}
    	
    	if(endTime != null && !endTime.trim().isEmpty()) {
    		Date endDate = TimeUtility.getDate(endTime, "yyyy-MM-dd HH:mm");
    		data.put("endYear", "   " + TimeUtility.getFormatTime(endDate, "yyyy") + "   ");
    		data.put("endMonth", "   " + TimeUtility.getFormatTime(endDate, "MM") + "   ");
    		data.put("endDay", "   " + TimeUtility.getFormatTime(endDate, "dd") + "   ");
    		data.put("endHour", "   " + TimeUtility.getFormatTime(endDate, "HH") + "   ");
    		data.put("endMin", "   " + TimeUtility.getFormatTime(endDate, "mm") + "   ");
    	}
		
    	if(address != null) {
    		data.put("address", address);
    	}
    	
    	if(senderArray != null && senderArray.size() > 0) {
    		NumberingBuilder builder = Numberings.of(NumberingFormat.DECIMAL);
    		for(int index = 0; index < senderArray.size(); index++) {
    			JSONObject sender = senderArray.getJSONObject(index);
    			String senderName = sender.getString("senderName");
    			if(senderName != null && senderName.trim().length() > 0) {
    				builder.addItem(senderName + "、" + sender.getString("company"));
    			}
    		}
    		data.put("senderList", builder.create());
    	}
    	
    	if(tools != null && tools.size() > 0) {
    		ParagraphRenderData toolsParagraph = new ParagraphRenderData();
    		for(int index = 0;index < tools.size();index++) {
    			JSONObject tool = tools.getJSONObject(index);
    			String toolType = tool.getString("toolType");
    			if(toolType == null || toolType.trim().length() == 0) {
    				continue;
    			}
    			toolsParagraph.addText("    " + toolType + "\n");
    			
    			JSONArray subTools = tool.getJSONArray("subTools");
    			if(subTools != null && subTools.size() > 0) {
    				for(int inner = 0;inner < subTools.size();inner++) {
    					JSONObject subTool = subTools.getJSONObject(index);
    					String toolName = subTool.getString("toolName");
    					if(toolName == null || toolName.trim().length() == 0) {
    	    				continue;
    	    			}
    					if(inner == (subTools.size() - 1) && index == (tools.size() - 1)) {
    						toolsParagraph.addText("        " + toolName);
    					}else {
    						toolsParagraph.addText("        " + toolName + "\n");
    					}
    					
    				}
    			}
    		}
    		data.put("tools", toolsParagraph);
    	}
    	
    	if(inspectObject != null) {
    		data.put("inspectObject", inspectObject);
    	}
    	
    	if(inspectObjectDesc != null) {
    		data.put("inspectObjectDesc", inspectObjectDesc);
    	}
    	
    	if(inspectMethods != null && inspectMethods.size() > 0) {
    		NumberingBuilder builder = Numberings.of(NumberingFormat.DECIMAL);
    		for(int index = 0; index < inspectMethods.size(); index++) {
    			JSONObject inspectMethod = inspectMethods.getJSONObject(index);
    			String methodDesc = inspectMethod.getString("methodDesc");
    			if(methodDesc != null && methodDesc.trim().length() > 0) {
    				builder.addItem(methodDesc);
    			}
    		}
    		data.put("inspectMethods", builder.create());
    	}
    	
    	if(inspectResult != null) {
    		data.put("inspectResult", inspectResult);
    	}

    	if(commander != null) {
    		data.put("commander", commander);
    	}
    	
    	if(scouter != null) {
    		data.put("scouter", scouter);
    	}
    	
    	if(recorder != null) {
    		data.put("recorder", recorder);
    	}
    	
    	Configure config = Configure.builder().bind("tools", new ParagraphRenderPolicy()).build();
		ResponseUtility.response(response, "bilu", System.currentTimeMillis() + ".docx", data, config);
    }
}
